<template>
  <div id="app">
    <div class="app-container">
      <div class="content1">
        <div class="card" style="float: left">
          <div class="img"><img src="../../assets/imgs/code编码.png" style="width: 83px; height: 83px; border-radius: 50%;"></div>
          <h1>摩斯编码</h1>
          <p><el-button type="success" size="mini" @click="openDialog()">GO</el-button></p>
        </div>

        <div class="card" style="float: left">
          <div class="img"><img src="../../assets/imgs/解码器.png" style="width: 83px; height: 83px; border-radius: 50%;"></div>
          <h1>摩斯解码</h1>
          <p><el-button type="success" size="mini" @click="dialogFormVisible1=true">GO</el-button></p>
        </div>

        <el-dialog
          :visible.sync="dialogFormVisible"
          title="请输入内容">
          <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 100%; margin-left: -50px;">
            <el-form-item prop="textarea">
              <el-input
                v-model="temp.text"
                type="textarea"
                placeholder="这里输入的内容将被摩斯编码（1-300字）"
                maxlength="300"
                rows="6"
                class="text-input"
              >
              </el-input>
            </el-form-item>
            <div class="encode-morse">摩斯电码：</div>
            <div class="encode-morse" v-if="encodeText != ''">{{encodeText}}</div>
            <el-form-item>
              <el-button type="primary" @click="encodeMorse()">确认</el-button>
              <el-button @click="dialogFormVisible=false">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>

        <el-dialog
          :visible.sync="dialogFormVisible1"
          title="请输入内容">
          <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 100%; margin-left: -50px;">
            <el-form-item prop="textarea">
              <el-input
                v-model="temp.morseText"
                type="textarea"
                placeholder="这里输入摩斯码"
                maxlength="300"
                rows="6"
                class="text-input"
              >
              </el-input>
            </el-form-item>
            <div class="encode-morse">解码：</div>
            <div class="encode-morse" v-if="decodeText != ''">{{decodeText}}</div>
            <el-form-item>
              <el-button type="primary" @click="decodeMorse()">确认</el-button>
              <el-button @click="dialogFormVisible1=false">取消</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {encodeMorse, decodeMorse} from '@/api/text'


export default {
  name: 'text',
  data() {
    return {
      dialogFormVisible: false,
      dialogFormVisible1: false,
      temp: {
        text: '',
        morseText: ''
      },
      rules: {
        text: [
          {required: true, message: '请输入二维码内容', trigger: 'blur'},
          {min: 1, max: 300, message: '长度在1~300个字符', trigger: 'blur'}
        ]
      },
      encodeText: '',
      decodeText: ''
    }
  },
  methods: {
    openDialog () {
      this.dialogFormVisible = true
    },
    encodeMorse () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          encodeMorse(tempData).then(response => {
            if (response.data.code === 20000) {
              this.encodeText = response.data.data
            } else {
              this.$message({
                message: response.data.message,
                type: 'error'
              })
            }
          })
        }
      })
    },
    decodeMorse () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          decodeMorse(tempData).then(response => {
            if (response.data.code === 20000) {
              this.decodeText = response.data.data
            } else {
              this.$message({
                message: response.data.message,
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.encode-morse {
  margin-left: 100px;
  margin-bottom: 20px;
}
.card {
  width: 190px;
  height: 240px;
  background: #1b7ea8;
  border-radius: 16px;
  text-align: center;
  color: #f0f0f0;
  font-family: inherit;
  z-index: 1;
  margin-right: 16px;
}
.img {
  display: flex;
  margin: 60px auto 10px auto;
  width: 85px;
  height: 85px;
  border: 1px solid #f0f0f0;
  border-radius: 50%;
  font-size: 11px;
  justify-content: center;
  align-items: center;
  transition: all 0.5s;
  z-index: 99;
}
p{
  opacity: 0;
  transition: all 0.5s;
}
h1{
  padding: 10px;
  font-weight: 500;
  font-size: 18px;
  color: white;
}
.card:hover .img {
  transform: scale(1.1);
  border: 1px solid #f0f0f0;
}
.card:hover h1,
.card:hover p {
  opacity: 1;
}
</style>
